<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-09-24 10:50:08
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Repositories\FinanceVoucherRepository;
use App\Traits\AttachmentTrait;
use App\Traits\AutoCodeTrait;
use App\Traits\VoucherTrait;

class FinanceCharge extends BaseModel
    {
    use AttachmentTrait;
    use VoucherTrait;
    use AutoCodeTrait;
    public $timestamps = true;
    public $table = "sys_finance_charge";
    protected $guarded = [];
    protected $ACSign = 'FINANCE_CHARGE';
    protected $casts = [];
    public function subject()
        {
        return $this->belongsTo(Subject::class);
        }
    public function account()
        {
        return $this->belongsTo(Subject::class);
        }
    public function department()
        {
        return $this->belongsTo(FinanceDepartment::class);
        }
    public function company()
        {
        return $this->belongsTo(Company::class);
        }
    public function tax()
        {
        return $this->belongsTo(Dic::class, "tax_id", "key")->where(function ($q)
            {
            $q->where("code", 'FinanceTax');
            });
        }
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    public function trade()
        {
        return $this->belongsTo(Trade::class);
        }


    public function finance($orders = null)
        {

        $gdata = $orders ?? $this;

        if ($gdata->status_id == 2)
            return $gdata->voucher;
        if ($gdata->status_id != 1) {
            fail("单据非审核状态！，无法财审！");
            }

        if (isEmpty($gdata->summary)) {
            $summary = ($gdata->state == 1 ? "其他收入-" : "其他支出-") . "{$gdata->subject->name} {$gdata->amount}元";
            }
        else
            $summary = $gdata->summary;

        $details = [];
        $data    = [
            'financedepartment_id' => $gdata->department_id,
            ...$gdata->toArray()
        ];
        //其他收款
        $tax_subject = "";
        if ($gdata->state == 1) {
            if ($gdata->tax_id == 1)
                $tax_subject = "1122";
            //借 银行存款
            $details[] = FinanceVoucherRepository::mergeVoucher($gdata->account_id, $summary, $gdata->amount, 0, $data, 0, $gdata->tax_id);
            //贷 某科目
            $details[] = FinanceVoucherRepository::mergeVoucher($gdata->subject_id, $summary, 0, $gdata->amount, $data, 0, $gdata->tax_id);

            }
        //其他付款
        else if ($gdata->state == 2) {
            if ($gdata->tax_id == 1)
                $tax_subject = "2202";
            //借 某科目
            $details[] = FinanceVoucherRepository::mergeVoucher($gdata->subject_id, $summary, $gdata->amount, 0, $data, 0, $gdata->tax_id);
            //贷 银行存款
            $details[] = FinanceVoucherRepository::mergeVoucher($gdata->account_id, $summary, 0, $gdata->amount, $data, 0, $gdata->tax_id);
            }
        //有税金，走往来，记票
        if ($tax_subject != "") {
            if (isEmpty($gdata->company_id)) {
                fail("还是必须选择往来单位");
                }
            $details[] = FinanceVoucherRepository::mergeVoucher($tax_subject, $summary, 0, 0, array_merge($gdata->toArray(), [
                'material_id'    => 0,
                'quantity'       => 0,
                'price'          => 0,
                'invoice_amount' => $gdata->amount
            ]), 0, $gdata->tax_id);
            }
        $voucher = FinanceVoucherRepository::CreateVoucher($gdata->billdate, 1, $details, $gdata, $gdata->voucher?->id);
        auditSave($gdata, true, true, 2);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }
    }
